package client

import (
	"context"
	"github.com/golang/protobuf/ptypes/wrappers"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"
	"kop/framework/wrapper"
	"kop/pb"
	"kop/pb/service"
)

var MoneyService service.MoneyService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	//s.Init(test.Flags()...)

	MoneyService = service.NewMoneyService("go.micro.srv.money", c)
}

func AddCoin(roleID int32, coin int64) *pb.Money {

	var money, err = MoneyService.AddCoin(context.Background(), &service.CoinRequest{RoleID: roleID, Coin: coin})
	if err != nil {
		panic(err)
	}

	return money
}

func SubCoin(ctx context.Context, coin int64) (*pb.Money, error) {

	return MoneyService.SubCoin(ctx, &wrappers.Int64Value{Value: coin})
}

func AsyncSubCoin(ctx context.Context, coin int64) error {

	var _, err = MoneyService.AsyncSubCoin(ctx, &wrappers.Int64Value{Value: coin})
	return err
}

func GetIncome(roleID int32) int64 {

	var resp, err = MoneyService.GetIncome(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.Value
}

// 更新每秒银币收益
func UpdateIncome(roleID int32, coin int64) error {

	var _, err = MoneyService.UpdateIncome(context.Background(), &service.CoinRequest{RoleID: roleID, Coin: coin})
	return err
}
